<template>

</template>
<script>
  import { fetchUserInfo } from '@/api/common'
  export default {
    data () {
      return {
      }
    },
    created () {
      // 正式环境
      this.autoLogin(true)

      // 测试环境
      // this.autoLogin(false)
    },
    methods: {
      autoLogin (isAuto) {
        var router = this.$router
        if (isAuto && this.$cookie.get('netopstec_token') === null) {
          window.dd.ready(function () {
            window.dd.runtime.permission.requestAuthCode({
              corpId: 'dinga7e92f372dba0344', // 企业id
              onSuccess: function (info) {
                fetchUserInfo({code: info.code}).then(response => {
                  var date = new Date()
                  date.setTime(date.getTime() + 1000 * 3600 * 6) // 设置cookie超时时间为6小时
                  document.cookie = 'netopstec_token=' + response.data.token + ';expires=' + date.toGMTString()
                  router.push({path: '/studentScore' + window.location.hash.substring(window.location.hash.indexOf('?id='))})
                })
              },
              onFail: function (err) {
                console.log('requestAuthCode fail: ' + JSON.stringify(err))
              }
            })
          })
        } else {
          router.push({path: '/studentScore' + window.location.hash.substring(window.location.hash.indexOf('?id='))})
        }
      }
    }
  }
</script>
